package com.tuanzi.loan.business.service.system.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tuanzi.loan.business.entity.system.Partner;
import com.tuanzi.loan.business.repository.system.PartnerRepository;
import com.tuanzi.loan.business.service.system.PartnerService;
import com.tuanzi.loan.business.service.system.SystemUserService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.repository.JPABaseRepository;
import com.tuanzi.loan.core.service.BaseServiceImpl;
import com.tuanzi.loan.core.vo.PageSearcher;
import com.tuanzi.loan.vo.request.system.PartnerSearchRequest;
import com.tuanzi.loan.vo.response.system.PartnerSearchResponse;

/**
 * @author Ray 2017/09/21
 */
@Debug
@Service
@Transactional
@Retryable(value = { RetryException.class })
public class PartnerServiceImpl extends BaseServiceImpl<Partner, String> implements PartnerService {

    @Autowired
    private PartnerRepository partnerRepository;
    @Autowired
    private SystemUserService userService;

    @Override
    protected JPABaseRepository<Partner, String> getRepository() {
        return partnerRepository;
    }

    @Override
    public Page<PartnerSearchResponse> list(PageSearcher<PartnerSearchRequest> pageSearcher) {
        return partnerRepository.list(pageSearcher.getSearchCondition(), pageSearcher.getPageable());
    }

    @Override
    public Partner disable(Partner partner) {
        userService.findAllByPartnerId(partner.getId()).stream().forEach(userService::disable);
        return super.disable(partner);
    }
}
